<?php

namespace app\mobile\controller;

use think\Controller;
use think\Lang;
use think\Request;
use org\Agent;
use org\Ip;
class Base extends Controller
{

    protected $template, $system, $datas,$url;

    public function _initialize()
    {
        /**
         * 根据ip获取城市、网络运营商等信息
         */

        /*
        $taobaodata = cache('\'' . $this->request->ip() . '\'');
        if (empty($taobaodata)) {
            $taobaodata = @file_get_contents('http://ip.taobao.com/service/getIpInfo.php?ip=' . $this->request->ip());
            $taobaodata = json_decode($taobaodata, $assoc = true);
            cache('\'' . $this->request->ip() . '\'', $taobaodata);
        }
        */

        $this->datas['status'] = '1';
        $this->datas['lang'] = cookie('think_var') ? cookie('think_var') : 'zh-cn';

        $iptime = cache('time\'' . $this->request->ip() . '\'');
        if(empty($iptime)){
            cache('time\'' . $this->request->ip() . '\'',true,300);
//            if($taobaodata){
//                $data['dizhi'] = $taobaodata['data']['country'] . $taobaodata['data']['region'] . $taobaodata['data']['city'];
//                $data['ip'] = $taobaodata['data']['ip'];
//                $data['lang'] = $taobaodata['data']['country_id'];
//            }

            $data['dizhi'] = implode(" ", Ip::find($this->request->ip()));
            $data['ip'] = $this->request->ip();

            $http = $this->request->server('HTTP_REFERER');
            if (!empty($http)) {
                $key = keytype($http);
                if ($key) {
                    $data['antepage'] = $key['1'];
                    $data['network'] = $key['0'];
                }
                $url = $http;
            } else {
                $url = $this->request->url(true);
            }
            //添加浏览人数
            db('system')->where('title','const')->where('lang',$this->datas['lang'])->setInc('value');
            $data['visitpage'] = $url;
    //        $browser = getbrowser();
            $data['browser'] = Agent::getBroswer();//$browser['bro'];
            $data['sys'] = Agent::getOs();//$browser['sys'];
            model('VisitSummary')->allowField(true)->save($data);
        }
        //记录浏览人数
        $const = db('system')->where('title','const')->where('lang',$this->datas['lang'])->value('value');
        $dirname= $this->request->module() =='index'?'pc':'mobile';
        $dirname='pc';
        $this->template = APP_PATH.request()->module().'/../../template/'.$dirname . DS.cache('systemsystem' . cookie('think_var'))[$dirname.'template']['value'] . DS;
//        $this->template = '../template/'.$dirname . DS.cache('systemsystem' . cookie('think_var'))[$dirname.'template']['value'] . DS;
        $this->assign([
            'link'=>cache('link' . cookie('think_var')),
            'const'=>$const,
            'nav_model'=>config('nav.nav_type'),
            'empty' => lang('No relevant data'),
        ]);
    }

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->url=$this->request->module()=='mobile'?'/mobile':'';
        $this->url=$this->request->domain().$this->url;
        //头部导航
        $navs = model('nav')->catenav($this->datas);

        $json_navs = json_encode($navs);
/*
        //底部导航
        $nav = cache('nav' . cookie('think_var'));
        if (empty($nav)) {
            $nav = model('nav')
                ->where('pid', '0')
                ->where($this->datas)
                ->order('sort ASC,id ASC')
                ->limit(10)->select();
            cache('nav' . cookie('think_var'), $nav);
        }
*/
        //在线帮助
        $online = cache('online' . cookie('think_var'));
        if (empty($online)) {
            $online = model('online')
                ->where($this->datas)
                ->select();
            cache('online' . cookie('think_var'), $online);
        }

        //幻灯片
        $navid = $this->request->param('navid');
        $navid = $navid ? $navid : '0';
        $carousel = cache('\'' . $navid . '\'' . cookie('think_var'));
        if (empty($carousel)) {
           $where['model'] = $this->request->module();
            isset($navid) ? $where['type'] = ['like', '%,' . $navid . ',%'] : '';
            $carousel = model('carousel')
                ->where($where)
                ->where($this->datas)
                ->select();
            cache('\'' . $navid . '\'' . cookie('think_var'), $carousel);
        }

        //幻灯片
        $centerturn = cache('turn\'' . $navid . '\'' . cookie('think_var'));
        if (empty($centerturn)) {
            $where = null;
            isset($navid) ? $where['type'] = ['like', '%,' . $navid . ',%'] : '';
            $centerturn = model('centerturn')->where($where)->where($this->datas)->order('sort deSC')->limit(4)->select();
            cache('turn\'' . $navid . '\'' . cookie('think_var'), $centerturn);
        }
        $this->assign([
            'carousel' => $carousel,
            'centerturn' => $centerturn,
            'online' => $online,
            'json_navs' => $json_navs,
            'nav' => $navs,
            'system' => cache('systemsystem' . cookie('think_var')),
        ]);
    }


    protected $beforeActionList = [
        'right' => ['only' => 'index,detailed'],
    ];

    public function index($navid)
    {

        if (!$navid) {
            $this->redirect($this->url);
//            $this->error('参数错误');
        }

        $cateInfo = cache('cateInfo'.$navid);
        if(empty($cateInfo)){
        /*栏目详情start*/
        $cateInfo = db('nav')->where('id', $navid)->where($this->datas)->find();

            cache('cateInfo'.$navid,$cateInfo);
        }

        if (!$cateInfo) {
            if(!empty($this->request->param('navid'))){
                $lang= model('nav')->where(['id'=>$this->request->param('navid')])->value('lang');
                if($lang != cookie('think_var')&&$lang){
                    cookie('think_var',$lang);
                    $this->redirect($this->request->url(true));
                }
            }
            $this->redirect($this->url);
            //$this->error('栏目不存在');
        }
        /*栏目详情end*/
        $this->assign('cateInfo', $cateInfo);

        /*模板start*/
        if ($cateInfo['type'] == 'one') {

            if (empty($cateInfo['pid'])){

                if(empty(cache('nav_top_one'.$navid. cookie('think_var')))) {
                    $cateInfo_new = db('nav')
                        ->where('pids', 'like', '%,' . $navid . ',%')
                        ->where($this->datas)
                        ->where('type','one')
                        ->where('id','neq',$navid)
                        ->order('sort deSC')
                        ->find();
                    cache('nav_top_one'.$navid. cookie('think_var'),$cateInfo_new);
                }

                if(cache('nav_top_one'.$navid. cookie('think_var'))){
                    $this->assign('cateInfo', cache('nav_top_one'.$navid. cookie('think_var')));
                }else{
                    $cateInfo_new = db('nav')
                        ->where('pids', 'like', '%,' . $navid . ',%')
                        ->where($this->datas)
                        ->where('type','model')
                        ->where('id','neq',$navid)
                        ->order('sort deSC')
                        ->find();
                    if(!empty($cateInfo_new)){
                        return $this->index($cateInfo_new['id']);
                    }
                }

            }

            /*文章列表end*/
            return $this->fetch($this->template . $cateInfo['temppage']);
        } else {
            if ($cateInfo['pid']) {
                $list = cache($cateInfo['id'].$this->request->param('page').$this->request->module().cookie('think_var'));
                if(empty($list)){
                /*所有数据列表start*/
                $list = db($cateInfo['model'])->alias('a')
                    ->join('__NAV__ c', 'c.id= a.cid')
                    ->field('a.*,c.name,c.model')
                    ->where('c.status', '1')
                    ->where('a.status', '1')
                    ->where('a.lang = c.lang')
                    ->where('a.lang',cookie('think_var'))
                    ->order('a.sort DESC,a.id DESC')
                    ->where('c.id', $navid)
                    ->paginate()
                    ->appends($this->request->param());

                    cache($cateInfo['id'].$this->request->param('page').$this->request->module(). cookie('think_var'),$list);
                }
            } else {
                /*列表start*/
                $list = cache($cateInfo['id']. $this->request->param('page').$this->request->module().cookie('think_var'));
                if(empty($list)) {
                    $list = db($cateInfo['model'])->alias('a')
                        ->join('__NAV__ c', 'c.id = a.cid')
                        ->field('a.*,c.name,c.model')
                        ->where('c.status', '1')
                        ->where('c.model', $cateInfo['model'])
                        ->where('a.status', '1')
                        ->where('a.lang = c.lang')
                        ->where('a.lang', cookie('think_var'))
                        ->order('a.sort DESC,a.id DESC')
                        ->where('c.pids', 'like', '%,' . $navid . ',%')
                        ->paginate()
                        ->appends($this->request->param());
                    cache($cateInfo['id'].$this->request->param('page').$this->request->module(). cookie('think_var'),$list);
                }

            }
            $this->assign('list', $list);

            /*列表end*/
            return $this->fetch($this->template . $cateInfo['templist']);
        }
        /*模板end*/
    }


    public function detailed($id)
    {
        //显示内容
        $model = $this->request->controller();
        $data = cache($model.$id);
        if(empty($data)){
            $data = model($model)->where($this->datas)->find($id);
            cache($model.$id,$data);

        }

        $cateInfo = db('nav')->where('id', $data['cid'])->where($this->datas)->find();
        if (!$cateInfo) {
            $this->redirect($this->url);
//            $this->error(Lang::get('error'));
        }
        //更多页面
        !empty($data['cid']) ? $datas['cid'] = $data['cid'] : '';
        $datas['status'] = '1';
        $datas['lang'] = cookie('think_var');
        $next = model($model)->getnext($id, $datas);
        $this->assign('data', $data);
        $this->assign('cateInfo', $cateInfo);
        $this->assign('next', $next);
        return $this->fetch($this->template . $cateInfo['temparticle']);
    }

    /**
     * 更多页面
     */
    public function more(){
        $table = $this->request->controller();
        $list=model($table)->where($this->datas)->paginate();
        $reight_nav = model('nav')->where('model', $table)->where($this->datas)->select();

        $this->assign([
            'list' => $list,
            'table' => $table,
            'reight_nav' => $reight_nav,
            'fname_nav' => Lang::get('All class'),
            'empty' => lang('No relevant data'),
        ]);
        return $this->fetch($this->template.'more.html');
    }

    //左边导航
    public function right()
    {
        $pid = model('nav')->where('id', $this->request->param('navid'))->where($this->datas)->value('pid');
        $pid = !empty($pid) ? $pid : $this->request->param('navid');

        $reight_nav = cache('right'.$pid.cookie('think_var'));
        if(empty($reight_nav1)){
            $reight_nav = model('nav')
                ->where('pid', $pid)
                ->where($this->datas)
                ->order('sort asc,id ASC')
                ->select();
            $reight_nav= $reight_nav?$reight_nav:'null';
            cache('right'.$pid.cookie('think_var'),$reight_nav);
        }
        $id = !empty($reight_nav[0]['pid']) ? $reight_nav[0]['pid'] : $this->request->param('navid');
        $fname_nav = model('nav')->where('id', $id)->where($this->datas)->value('name');

        $this->assign('reight_nav', $reight_nav);
        $this->assign('fname_nav', $fname_nav);
    }

    //左边导航_备份
    public function right_bloak()
    {

        $pid = model('nav')->where('id', $this->request->param('navid'))->where($this->datas)->value('pid');
        $pid = !empty($pid) ? $pid : $this->request->param('navid');
        $reight_nav = model('nav')
            ->where('pids', 'like', '%,' . $pid . ',%')
            ->where($this->datas)
            ->whereNotIn('id', $this->request->param('navid'))
            ->order('sort asc,id ASC')
            ->select();
        $this->assign('reight_nav', $reight_nav);
    }

}
